package com.base.recycleView.adapter;

/**
 * Created by gml on 17/5/25.
 */

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.base.recycleView.activity.BindingViewHolder;
import com.gml.gao.R;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * Created by Administrator on 16-7-18.
 */
public abstract class BaseReceycleAdapter<T> extends RecyclerView.Adapter<BindingViewHolder> {

    protected List<T> mEmployeeList;


    private static final int ITEM_VIEW_TYPE_ON = 1;
    private static final int ITEM_VIEW_TYPE_OFF = 2;

    protected final LayoutInflater mLayoutInflater;

    private OnItemClickListener mListener;


    public BaseReceycleAdapter(Context context) {
        mEmployeeList = new ArrayList<>();
        mLayoutInflater = (LayoutInflater)
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    public interface OnItemClickListener {
        <T> void onEmployeeClick(T employee);
    }


    @Override
    public BindingViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewDataBinding binding;

//        if (viewType == ITEM_VIEW_TYPE_ON) {
        binding = DataBindingUtil.inflate(mLayoutInflater,
                getInflaterViewId(), parent, false);
//        } else {
//            binding = DataBindingUtil.inflate(mLayoutInflater,
//                    R.layout.item_employee_off, parent, false);
//        }
        return new BindingViewHolder(binding);
    }

   public  abstract int getInflaterViewId();

    @Override
    public void onBindViewHolder(BindingViewHolder holder, int position) {
        final T employee = mEmployeeList.get(position);
        holder.getBinding().setVariable(com.gml.gao.BR.item, employee);
        holder.getBinding().executePendingBindings();
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onEmployeeClick(employee);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mEmployeeList.size();
    }

//    @Override
//    public int getItemViewType(int position) {
//        final User employee = (User) mEmployeeList.get(position);
//        if (employee.isFired.get()) {
//            return ITEM_VIEW_TYPE_OFF;
//        } else {
//            return ITEM_VIEW_TYPE_ON;
//        }
//    }


    public void add(T employee) {
        mEmployeeList.add(employee);
        notifyDataSetChanged();
    }

    public void remove(T employee) {
        mEmployeeList.remove(employee);
        notifyDataSetChanged();
    }


    public void addAll(ArrayList<T> employees) {
        mEmployeeList.addAll(employees);
        notifyDataSetChanged();
    }

}
